<template>
  <a-layout-header class="header">
    <!-- 二进制代码纹理背景 -->
    <div class="binary-texture"></div>
    <!-- 光纤效果分隔线 -->
    <div class="fiber-line top-line"></div>
    <div class="fiber-line bottom-line"></div>

    <a-row :wrap="false" class="header-content">
      <!-- 左侧：Logo和标题 -->
      <a-col flex="240px">
        <RouterLink to="/">
          <div class="header-left">
            <img class="logo" src="@/assets/logo.png" alt="Logo" />
            <h1 class="site-title">木棉织码云端</h1>
          </div>
        </RouterLink>
      </a-col>

      <!-- 中间：导航菜单 -->
      <a-col flex="auto">
        <div class="menu-container">
          <a-menu
            v-model:selectedKeys="selectedKeys"
            mode="horizontal"
            :items="menuItems"
            @click="handleMenuClick"
            class="custom-menu"
          />
        </div>
      </a-col>

      <!-- 右侧：用户操作区域 -->
      <a-col>
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id" class="user-info-wrapper">
            <a-dropdown>
              <a-space class="user-info">
                <a-avatar :src="loginUserStore.loginUser.userAvatar" class="user-avatar" />
                <span class="user-name">{{ loginUserStore.loginUser.userName ?? '无名' }}</span>
              </a-space>
              <template #overlay>
                <a-menu class="dropdown-menu">
                  <a-menu-item @click="doLogout" class="logout-item">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else class="login-wrapper">
            <a-button type="primary" href="/user/login" class="login-btn">
              <span class="login-text">登录</span>
              <div class="btn-glow"></div>
            </a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </a-layout-header>
</template>

<script setup lang="ts">
import { computed, h, ref } from 'vue'
import { useRouter } from 'vue-router'
import { type MenuProps, message } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { userLogout } from '@/api/userController.ts'
import { LogoutOutlined, HomeOutlined } from '@ant-design/icons-vue'

const loginUserStore = useLoginUserStore()
const router = useRouter()
// 当前选中菜单
const selectedKeys = ref<string[]>(['/'])
// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, next) => {
  selectedKeys.value = [to.path]
})

// 菜单配置项
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/appManage',
    label: '应用管理',
    title: '应用管理',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    const menuKey = menu?.key as string
    if (menuKey?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const menuItems = computed<MenuProps['items']>(() => filterMenus(originItems))

// 处理菜单点击
const handleMenuClick: MenuProps['onClick'] = (e) => {
  const key = e.key as string
  selectedKeys.value = [key]
  // 跳转到对应页面
  if (key.startsWith('/')) {
    router.push(key)
  }
}

// 退出登录
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
.header {
  background: linear-gradient(135deg, #98e4b8 0%, #7dd3c0 50%, #42b883 100%);
  padding: 0 40px;
  box-shadow: 0 4px 20px rgba(66, 184, 131, 0.15);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
  height: 64px;
}

/* 二进制代码纹理背景 */
.binary-texture {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    radial-gradient(circle at 20% 80%, rgba(255, 255, 255, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.05) 0%, transparent 50%),
    linear-gradient(45deg, transparent 40%, rgba(255, 255, 255, 0.02) 50%, transparent 60%);
  background-size: 200px 200px, 150px 150px, 50px 50px;
  animation: binaryFlow 20s linear infinite;
  pointer-events: none;
}

.binary-texture::before {
  content: '01001001 01101110 01101110 01101111 01110110 01100001 01110100 01101001 01101111 01101110';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  font-family: 'Courier New', monospace;
  font-size: 8px;
  color: rgba(255, 255, 255, 0.03);
  white-space: nowrap;
  overflow: hidden;
  animation: binaryScroll 30s linear infinite;
  line-height: 64px;
  letter-spacing: 2px;
}

@keyframes binaryFlow {
  0% { transform: translateX(-100px) translateY(-50px); }
  100% { transform: translateX(100px) translateY(50px); }
}

@keyframes binaryScroll {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

/* 光纤效果分隔线 */
.fiber-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.3) 20%,
    rgba(103, 217, 168, 0.6) 50%,
    rgba(255, 255, 255, 0.3) 80%,
    transparent 100%);
  animation: fiberPulse 3s ease-in-out infinite;
}

.top-line {
  top: 0;
  animation-delay: 0s;
}

.bottom-line {
  bottom: 0;
  animation-delay: 1.5s;
}

@keyframes fiberPulse {
  0%, 100% { opacity: 0.3; transform: scaleX(0.8); }
  50% { opacity: 1; transform: scaleX(1.2); }
}

/* 纺织纹理效果 */
.header::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    repeating-linear-gradient(45deg, transparent, transparent 2px, rgba(255, 255, 255, 0.02) 2px, rgba(255, 255, 255, 0.02) 4px),
    repeating-linear-gradient(-45deg, transparent, transparent 2px, rgba(255, 255, 255, 0.02) 2px, rgba(255, 255, 255, 0.02) 4px);
  pointer-events: none;
}

.header-content {
  position: relative;
  z-index: 2;
  height: 100%;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
  z-index: 3;
}

.logo {
  height: 48px;
  width: 48px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(66, 184, 131, 0.3);
  transition: all 0.3s ease;
}

.logo:hover {
  transform: scale(1.05) rotate(5deg);
  box-shadow: 0 4px 16px rgba(66, 184, 131, 0.5);
}

.site-title {
  margin: 0;
  font-size: 22px;
  color: #ffffff;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  letter-spacing: 0.5px;
  position: relative;
}

.site-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.header-left:hover .site-title::after {
  transform: scaleX(1);
}

/* 菜单容器居中 */
.menu-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

/* 自定义菜单样式 */
:deep(.custom-menu) {
  background: transparent !important;
  border-bottom: none !important;
  line-height: 64px;
  display: flex;
  justify-content: center;
  min-width: 0;
  flex: 1;
}

:deep(.custom-menu .ant-menu-item) {
  color: #ffffff !important;
  font-weight: 500;
  font-size: 14px;
  margin: 0 16px;
  padding: 0 12px;
  border-radius: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  border: 1px solid transparent;
  white-space: nowrap;
  min-width: auto;
  flex-shrink: 0;
}

:deep(.custom-menu .ant-menu-item::before) {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

:deep(.custom-menu .ant-menu-item:hover::before) {
  left: 100%;
}

:deep(.custom-menu .ant-menu-item:hover) {
  background: rgba(179, 241, 210, 0.3) !important;
  color: #ffffff !important;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(179, 241, 210, 0.4);
  border-color: rgba(255, 255, 255, 0.3);
}

:deep(.custom-menu .ant-menu-item-selected) {
  background: rgba(103, 217, 168, 0.4) !important;
  color: #ffffff !important;
  box-shadow: 0 2px 8px rgba(103, 217, 168, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}

:deep(.custom-menu .ant-menu-item-selected::after) {
  border-bottom: 2px solid #ffffff !important;
}

:deep(.custom-menu .ant-menu-item .anticon) {
  color: #ffffff !important;
  transition: all 0.3s ease;
}

:deep(.custom-menu .ant-menu-item:hover .anticon) {
  transform: scale(1.1);
}

/* 用户信息区域 */
.user-login-status {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  height: 100%;
}

.user-info-wrapper {
  display: flex;
  align-items: center;
  height: 100%;
}

.user-info {
  color: #ffffff;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
}

.user-info:hover {
  background: rgba(179, 241, 210, 0.3);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(179, 241, 210, 0.4);
  border-color: rgba(255, 255, 255, 0.3);
}

.user-avatar {
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.user-info:hover .user-avatar {
  border-color: rgba(255, 255, 255, 0.8);
  transform: scale(1.05);
}

.user-name {
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* 登录按钮样式 */
.login-wrapper {
  display: flex;
  align-items: center;
  height: 100%;
}

.login-btn {
  background: linear-gradient(135deg, #67d9a8 0%, #42b883 100%) !important;
  border: none !important;
  color: #ffffff !important;
  font-weight: 600;
  border-radius: 8px;
  padding: 10px 24px;
  height: auto;
  box-shadow: 0 4px 12px rgba(103, 217, 168, 0.3);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
}

.login-text {
  position: relative;
  z-index: 2;
  font-size: 14px;
  letter-spacing: 0.5px;
}

.btn-glow {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  transition: left 0.6s;
}

.login-btn:hover .btn-glow {
  left: 100%;
}

.login-btn:hover {
  background: linear-gradient(135deg, #7dd3c0 0%, #30b77e 100%) !important;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(103, 217, 168, 0.5) !important;
  border-color: rgba(255, 255, 255, 0.6) !important;
}

.login-btn:active {
  transform: translateY(0);
}

/* 下拉菜单样式 */
:deep(.dropdown-menu) {
  background: rgba(255, 255, 255, 0.95) !important;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(66, 184, 131, 0.2);
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(66, 184, 131, 0.2);
}

:deep(.dropdown-menu .ant-menu-item) {
  color: #2d5a4a !important;
  transition: all 0.3s;
  border-radius: 6px;
  margin: 4px;
}

:deep(.dropdown-menu .ant-menu-item:hover) {
  background: rgba(179, 241, 210, 0.3) !important;
  color: #30b77e !important;
  transform: translateX(4px);
}

.logout-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1400px) {
  .header {
    padding: 0 32px;
  }

  :deep(.custom-menu .ant-menu-item) {
    margin: 0 12px;
    font-size: 14px;
  }
}

@media (max-width: 1200px) {
  .header {
    padding: 0 24px;
  }

  :deep(.custom-menu .ant-menu-item) {
    margin: 0 10px;
    padding: 0 10px;
    font-size: 13px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 16px;
  }

  .site-title {
    font-size: 18px;
  }

  :deep(.custom-menu .ant-menu-item) {
    margin: 0 6px;
    padding: 0 8px;
    font-size: 12px;
  }

  .login-btn {
    padding: 8px 16px;
  }

  .login-text {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .site-title {
    font-size: 16px;
  }

  .header-left {
    gap: 8px;
  }

  .logo {
    height: 40px;
    width: 40px;
  }

  :deep(.custom-menu .ant-menu-item) {
    margin: 0 4px;
    padding: 0 6px;
    font-size: 11px;
  }
}
</style>
